<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="app">
    <input type="checkbox" v-model="allChecked" @change="all">
    <span v-if="allChecked" @click="delAll">删除全部</span>
    <ul>
      <li v-for="item,index in arr" :key="item.id">
        <span><input type="checkbox" :value="item.id" v-model="ids" @change="changeItem"></span> --
        <span>{{item.title}}</span>
      </li>
    </ul>

  </div>

  <script>

    new Vue({
      el: '#app',
      data: {
        allChecked: false,
        ids: [],
        arr: [
          { id: 1, title: '杨洁被唐僧师徒踢出剧组' },
          { id: 2, title: '而契丹能够把北宋给收拾住，也确实有一定的历史脉络' },
          { id: 3, title: '这一件事都足以看出：在这次战争期间' },
        ]
      },
      methods: {
        all(evt) {
          // console.log(evt.target.checked)
          if (evt.target.checked) { // 选中时
            this.ids = this.arr.map(({ id }) => id)
          } else {
            this.ids = []
          }
        },
        changeItem(evt) {
          if (!evt.target.checked) {
            this.allChecked = false
          } else {
            // 判断当前数据是否和源数据长度一致
            this.allChecked = this.arr.length == this.ids.length
          }
        },
        delAll(){
          this.arr = []
        }
      }
    })
  </script>
</body>

</html>